<script>

    import swal from "sweetalert";

    export default {
        created() {
            // let appid = this.globalData.WX_APP_ID;
            // let url = this.globalData.HOTS + localStorage.getItem("now_url");
            // let code = this.getQueryVariable('code'); // 截取code
            // if (!code) {
            //     window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${encodeURIComponent(url)}&response_type=code&scope=snsapi_userinfo#wechat_redirect`
            //     return;
            // } else {
            //     this.api.openid(code).then(res => {
            //         // console.log(res);
            //         if (res.data.openid) {
            //             localStorage.setItem("wx_openid", res.data.openid);
            //             window.location.replace(url);
            //         } else {
            //             window.location.href = '/';
            //         }
            //         return;
            //     })
            // }


            let code = this.getQueryString("code");
            if (code) {

                let promote_code = localStorage.getItem("promote_code");
                if (promote_code) {
                    this.api.join(promote_code, code).then(res => {
                        localStorage.removeItem("promote_code");
                        if (res.meta.code == '-1' && res.data.openid) {
                            localStorage.setItem("wx_openid", res.data.openid);
                            swal(res.meta.msg).then((value) => {
                                window.location.replace('/center');
                            });
                        } else if (res.meta.code == '000' && res.data.openid) {
                            localStorage.setItem("wx_openid", res.data.openid);
                            window.location.replace('/signup');
                        } else {
                            window.location.href = '/';
                        }
                    });
                } else {
                    this.api.openid(code).then(res => {
                        // console.log(res);
                        if (res.data.openid) {
                            // 存储微信授权用户信息
                            localStorage.setItem("wx_openid", res.data.openid);

                            // update: 8.18检查是否绑定
                            if (!res.data.is_mobile_auth) {
                                window.location.replace('/signup');
                                return;
                            }

                            // 获取初次访问的URL地址
                            let url = localStorage.getItem("now_url");
                            // 重新打开指定页面
                            // this.$router.replace(redirectUrl);
                            window.location.replace(url);
                        } else {
                            window.location.href = '/';
                        }
                    })
                }

            } else {
                window.location.href = '/';
                // this.$router.replace("/");
            }
        },
        data() {
            return {};
        },
        methods: {
            // 获取地址栏参数
            getQueryString(n) {
                let r, reg = new RegExp("(^|&)" + n + "=([^&]*)(&|$)");
                r = window.location.search.substr(1).match(reg);
                return r ? decodeURI(r[2]) : null;
            },
            getQueryVariable(variable) {
                const query = window.location.search.substring(1)
                const vars = query.split("&")
                for (let i = 0; i < vars.length; i++) {
                    const pair = vars[i].split("=")
                    if (pair[0] === variable) return pair[1]
                }
                return undefined
            },
            // 截取code
            getUrlParame(parameName) {
                /// 获取地址栏指定参数的值
                /// <param name="parameName">参数名</param>
                // 获取url中跟在问号后面的部分
                var parames = window.location.search;
                // 检测参数是否存在
                if (parames.indexOf(parameName) > -1) {
                    var parameValue = '';
                    parameValue = parames.substring(parames.indexOf(parameName), parames.length)
                    // 检测后面是否还有参数
                    if (parameValue.indexOf('&') > -1) {
                        // 去除后面多余的参数, 得到最终 parameName=parameValue 形式的值
                        parameValue = parameValue.substring(0, parameValue.indexOf('&'))
                        // 去掉参数名, 得到最终纯值字符串
                        parameValue = parameValue.replace(parameName + '=', '')
                        return parameValue;
                    }
                }
                return '';
            }
        }
    };
</script>
